package br.com.jw.dao;

import java.io.DataInputStream;
import java.io.DataOutputStream;
import java.net.Socket;
import java.util.ArrayList;

import br.com.jw.modelo.Venda;
import android.content.Context;

public class VendaDao {
	String[] result;
	Context context;

	public VendaDao(Context context) {
		this.context = context;
	}
	
	 public ArrayList<Venda> getListaVendas(){
			
			try {
				
				Socket socket = new Socket("10.0.2.2", 7777);
				DataInputStream in = new DataInputStream(socket
						.getInputStream());
				DataOutputStream out = new DataOutputStream(socket
						.getOutputStream());
				
	            out.writeUTF("vendas");
				out.writeUTF("listarVendas");
				out.flush();
				result = in.readUTF().split(",");
				ArrayList<Venda> listaVendas = new ArrayList<Venda>();
				for (int i = 0; i < result.length; i+=4) {
					listaVendas.add(new Venda(
					        result[i],
							result[i+1], 
							Integer.parseInt(result[i+2]),
							result[i+3]));
						
				}									
				out.close();
				in.close();
				socket.close();
				return listaVendas;
			} catch (Exception e) {
				e.printStackTrace();
					// mostra os erros no log
				e.getMessage();
				return null;
			}	
		}
	
	public ArrayList<Venda> getListaVendaCliente(int cli_id){
		try {
			Socket socket = new Socket("10.0.2.2", 7777);
			DataInputStream in = new DataInputStream(socket
					.getInputStream());
			DataOutputStream out = new DataOutputStream(socket
					.getOutputStream());
            out.writeUTF("vendas");
			out.writeUTF("listarVendaCliente");
			out.writeInt(cli_id);
			
			out.flush();
			result = in.readUTF().split(",");
			
			ArrayList<Venda> listaVendaCliente = new ArrayList<Venda>();
			for (int i = 0; i < result.length; i+=4) {		
				listaVendaCliente.add(new Venda(
				        result[i],
						result[i+1], 
						Integer.parseInt(result[i+2]),
						result[i+3]));
			}					
			
			out.close();
			in.close();
			socket.close();
			return listaVendaCliente;
		} catch (Exception e) {
			e.printStackTrace();
				// mostra os erros no log
			e.getMessage();
			return null;
		}	
	}
	
	public boolean salvarVenda(Venda venda) {
		boolean resultado=false;
		try {
			Socket socket = new Socket("10.0.2.2", 7777);
			DataInputStream in = new DataInputStream(socket.getInputStream());
			DataOutputStream out = new DataOutputStream(
					socket.getOutputStream());
			out.writeUTF("vendas");
			out.writeUTF("salvarVendas");

			out.writeInt(venda.getVen_cli_id());			
			out.writeInt(venda.getVen_usu_id());
			out.writeInt(venda.getVen_pro_id());
			out.writeInt(venda.getVen_pro_qtde());

			out.flush();// envia os dados

			resultado = in.readBoolean();
			
			
			out.close();
			in.close();
			socket.close();
			return resultado;
			
		} catch (Exception e) {
			e.printStackTrace();
			// return false;
		}
		return resultado;
	}
	
	public boolean excluirVenda(Venda venda) {
		boolean resultado=false;
		try {
			Socket socket = new Socket("10.0.2.2", 7777);
			DataInputStream in = new DataInputStream(socket.getInputStream());
			DataOutputStream out = new DataOutputStream(
					socket.getOutputStream());
			out.writeUTF("vendas");
			out.writeUTF("excluirVendas");

			out.writeUTF(venda.getVen_nomeCliente());			
			out.writeUTF(venda.getVen_nomeProduto());
			//out.writeInt(venda.getVen_pro_qtde());
			out.writeUTF(venda.getVen_nomeUsuario());
			

			out.flush();// envia os dados

			resultado = in.readBoolean();
			
			
			out.close();
			in.close();
			socket.close();
			
			return resultado;
			
		} catch (Exception e) {
			e.printStackTrace();

		}
		return resultado;
	}
	

}
